package com.leyou.item.web;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import com.leyou.item.vo.BrandVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 品牌表，一个品牌下有多个商品（spu），一对多关系 前端控制器
 * </p>
 *
 * @author guoxi
 * @since 2020-12-06
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌
     * @param page 当前页码
     * @param rows  每页大小
     * defaultValue 默认大小，默认页码
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO> queryPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "key",required = false)String key
    ) {
      return ResponseEntity.ok(brandService.queryPage(page, rows,key));
    }


    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(brandService.queryBrandById(id));
    }


    /**
     * 根据品牌id集合查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandsByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(brandService.queryBrandsByIds(ids));
    }


    /**
     * 根据分类id查询品牌
     * @param cid
     * @return
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandsByCategoryId(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(brandService.queryBrandsByCategoryId(cid));
    }


    /**
     * 新增品牌
     * @param brandVo
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandVo brandVo) {
        //不用ResponseEntity.ok()的原因，ok返回的状态码是固定的200
        //前端要的是201，所以ResponseEntity.status()更适合
        //当ResponseEntity<Void>无返回值的时候就用build()即可

        this.brandService.addBrand(brandVo);
        return ResponseEntity.status(201).build();
    }


    /**
     * 更新品牌
     * @param brandVo
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandVo brandVo){
        this.brandService.updateBrand(brandVo);
        return ResponseEntity.status(201).build();
    }


    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id) {
        this.brandService.deleteBrand(id);
        return ResponseEntity.status(200).build();
    }
}

